﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Interface;

namespace Cashier.supplier
{
    public partial class SupplierListControl : UserControl, IDataGridViewData, IReloadData, IPrinter
    {
        private MainForm mainform;
        private SupplierService supplierService;
        private static SupplierListControl supplierListControl;

        /// <summary>
        /// 构造函数
        /// </summary>
        private SupplierListControl(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dataGridView1.ImplementsInstance = this;
            this.supplierService = new SupplierService();

            this.ControlsSetRights();
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 返回控件实例
        /// </summary>
        /// <returns></returns>
        public static SupplierListControl GetSupplierListControl(MainForm mainform)
        {
            if (supplierListControl == null)
                supplierListControl = new SupplierListControl(mainform);
            supplierListControl.RefreshDataGridViewData();
            return supplierListControl;
        }

        #region 功能方法实现

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_add, button_query };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            ContextMenuStrip[] menuStrips = { contextMenuStrip_dgv };
            RightsforControls.RightsforContextMenuStrip(mainform.Admin.RolePurviewtable, menuStrips);
        }

        /// <summary>
        /// 打开供货商信息窗口
        /// </summary>
        /// <param name="supplierid"></param>
        private void OpenSupplierAddForm(long supplierid)
        {
            SupplierAddForm supplierAddForm = new SupplierAddForm(mainform, this, null, supplierid);
            supplierAddForm.ShowDialog();
        }

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        private void DeleteSupplier()
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除选中数据吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
                    {
                        sb.Append(row.Cells[0].Value.ToString()).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    int count = this.supplierService.DeleteSupplier(sb.ToString());
                    if (count > 0)
                    {
                        MessageBox.Show("删除成功！");
                        this.RefreshDataGridViewData();
                    }
                    else
                        MessageBox.Show("删除失败！");
                }
            }
        }

        #endregion

        #region IDataGridViewData 成员

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String supplierno = textBox_supplierno.Text.Trim();
                String shortname = textBox_shortname.Text.Trim();
                DataTable dt = supplierService.GetSupplierTable(pageindex, pagesize, supplierno, shortname, String.Empty, "id");
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count>0)
                {                   
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }           
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String supplierno = textBox_supplierno.Text.Trim();
                String shortname = textBox_shortname.Text.Trim();
                record = supplierService.GetSupplierRecord(supplierno, shortname);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }           
            return record;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("supplier_no", "编号", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shortname", "简称", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("name", "联系人", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("position", "职务", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("mobile", "手机", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("company", "公司名称", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("supplier_no", "编号", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("createdate", "创建日期", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("salesman", "业务员", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
        }

        void IDataGridViewData.EnterkeyEventHandler()
        {
            try
            {
                if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                {
                    long supplierid = (long)dataGridView1.SelectedRows[0].Cells[0].Value;
                    this.OpenSupplierAddForm(supplierid);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 新增供货商信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_add_Click(object sender, EventArgs e)
        {
            this.OpenSupplierAddForm(0);
        }

        /// <summary>
        /// 查询供货商信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 文本框键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;
            if (keyValue == 13)
            {
                this.RefreshDataGridViewData();
            }
        }

        /// <summary>
        /// DataGridView 单元格双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                long supplierid = (long)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                this.OpenSupplierAddForm(supplierid);
            }
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        private void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 重载事件

        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool b = base.ProcessDialogKey(keyData);
            switch (keyData)
            {
                case Keys.F8:
                    this.RefreshDataGridViewData();
                    break;
                case Keys.F9:
                    this.OpenSupplierAddForm(0);
                    break;
            }
            return b;
        }

        #endregion

        #region IReloadData 成员

        void IReloadData.ReloadDataGridViewData(object param)
        {
            this.RefreshDataGridViewData();
        }

        #endregion

        #region IPrinter 成员

        DataGridView IPrinter.GetAllRows()
        {
            try
            {
                ((IDataGridViewData)this).GetBindingData(0, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return this.dataGridView1;
        }

        DataGridView IPrinter.GetSelectedRows()
        {
            return this.dataGridView1;
        }

        void IPrinter.VisibleColumn(string columnname)
        {
            try
            {
                if (this.dataGridView1.Columns[columnname].Visible)
                    this.dataGridView1.Columns[columnname].Visible = false;
                else
                    this.dataGridView1.Columns[columnname].Visible = true;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 右键菜单事件

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_add_Click(object sender, EventArgs e)
        {
            this.OpenSupplierAddForm(0);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                long supplierid = (long)this.dataGridView1.SelectedRows[0].Cells[0].Value;
                this.OpenSupplierAddForm(supplierid);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_delete_Click(object sender, EventArgs e)
        {
            this.DeleteSupplier();
        }

        #endregion
    }
}
